package com.zhss.springbootdemo01.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @ClassName MvcConfigurer
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2021/2/6 22:22
 * @Version 1.0
 */
@Configuration
public class MvcConfigurer implements WebMvcConfigurer {

	/**
	 * 拦截器
	 * @param registry
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new UserLoginInterceptor()).addPathPatterns("/user/**");
	}

	/**
	 * 跨域配置
	 * @param registry
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/user/**")
				.allowedOrigins("http://www.zhss.com")
				.allowedMethods("post", "get");
	}

	/**
	 * 格式化
	 * @param registry, 将数据从 json中提取出来, 如果有时间格式 , 使用格式化.
	 */
	@Override
	public void addFormatters(FormatterRegistry registry) {
		registry.addFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
	}


}
